﻿#ifndef WSListener_hpp
#define WSListener_hpp
#include "oatpp-websocket/ConnectionHandler.hpp"
#include "oatpp-websocket/WebSocket.hpp"

// websocket事件监听器
class WSListener 
	: public oatpp::websocket::WebSocket::Listener 
{

public:
	WSListener(std::mutex& writeMutex);

public:
	// 接收到ping帧
	void onPing(const WebSocket& socket, const oatpp::String& message) override;
	// 接收到pong帧
	void onPong(const WebSocket& socket, const oatpp::String& message) override;
	// 连接关闭事件
	void onClose(const WebSocket& socket, v_uint16 code, const oatpp::String& message) override;
	// 帧数据回调
	void readMessage(const WebSocket& socket, v_uint8 opcode, p_char8 data, oatpp::v_io_size size) override;

protected:
	// 消息处理
	void processMessage(std::string message);

private:
	// 客户标签
	static constexpr const char* TAG = "Client_WSListener";
	// 消息写锁
	std::mutex& m_writeMutex;
	// 消息缓存
	oatpp::data::stream::BufferOutputStream m_messageBuffer;
};

#endif